package com.mabeijianxi.smallvideorecord2;

import android.util.Log;
import com.mabeijianxi.smallvideorecord2.jniinterface.FFmpegBridge;

import java.io.*;

/**
 * Created by jianxi on 2017/6/5.
 * https://github.com/mabeijianxi
 * mabeijianxi@gmail.com
 */

public class JianXiCamera {
    /** 应用包名 */
    private static String mPackageName;
    /** 应用版本名称 */
    private static String mAppVersionName;
    /** 应用版本号 */
    private static int mAppVersionCode;
    /** 视频缓存路径 */
    private static String mVideoCachePath;

    /** 视频缓存路径 */

    public final static String FFMPEG_LOG_FILENAME = "ffmpeg.log";

    /** 执行FFMPEG命令保存路径 */
    public final static String FFMPEG_LOG_FILENAME_TEMP = "jx_ffmpeg.log";


    public static void init(String path,boolean isDebug){
        setVideoCachePath(path);
        initialize(isDebug);
    }
    /**
     *
     * @param debug debug模式
     */
    public static void initialize(boolean debug) {
        String logPath = null;
        if(debug){
            logPath=mVideoCachePath+"/"+FFMPEG_LOG_FILENAME_TEMP;
        }
        FFmpegBridge.initJXFFmpeg(debug,logPath);

    }


    /** 是否开启log输出 */
    public static boolean isLog() {
        return com.mabeijianxi.smallvideorecord2.util.Log.getIsLog();
    }

    /** 获取视频缓存文件夹 */
    public static String getVideoCachePath() {
        return mVideoCachePath;
    }

    /** 设置视频缓存路径 */
    public static void setVideoCachePath(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        mVideoCachePath = path;

    }

    /** 拷贝转码失败的log */
    protected static boolean copyFFmpegLog(String cmd) {
        boolean result = false;

        int size = 1 * 1024;

        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            File temp = new File(getVideoCachePath(), FFMPEG_LOG_FILENAME_TEMP);
            if (!temp.exists()) {
                temp.createNewFile();
                return false;
            }
            in = new FileInputStream(temp);
            out = new FileOutputStream(new File(getVideoCachePath(), FFMPEG_LOG_FILENAME), true);
            out.write("--------------------------------------------------\r\n".getBytes());
            out.write(cmd.getBytes());
            out.write("\r\n\r\n".getBytes());
            byte[] buffer = new byte[size];
            int bytesRead = -1;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }
            out.flush();
            result = true;
        } catch (FileNotFoundException e) {
            Log.e("upload", e.toString());
        } catch (IOException e) {
            Log.e("upload", e.toString());
        } catch (Exception e) {
            Log.e("upload", e.toString());
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
            }
        }
        return result;
    }
}
